package com.xzh.service;

import com.xzh.entity.Users;
import com.xzh.exception.CustomException;
import com.xzh.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class UsersService {

    @Resource
    private UserMapper usermapper;

    public Users selectUN(String username){
        return usermapper.selectUN(username);
    }

    //登录接口
    public Users login(Users users) {
        String username = users.getUserName();
        Users dpusers = usermapper.selectUN(username);
        if(dpusers == null){ //没有用户
            throw new CustomException("500","用户不存在");
        }
        String password = users.getPassword();
        if(!dpusers.getPassword().equals(password)){
            throw new CustomException("500","账号或密码错误");
        }
        return dpusers;
    }

    //注册接口
    public void register(Users users) {
        String username = users.getUserName();
        Users dpusers = usermapper.selectUN(username);
        if(dpusers != null){
            throw new CustomException("500","用户已经存在，请尝试返回登录");
        }
        usermapper.insert(users);
    }

    //编辑资料接口
    public void updatePerson(Users users) {
        usermapper.updatePerson(users);
    }

    public Users getUser(int userId) {
        return usermapper.getUser(userId);
    }
}
